using System;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;
using Newtonsoft.Json;
using SocialWidgetsContrib.Widgets.Shared;
using Telerik.Sitefinity.Web.UI;
using Telerik.Sitefinity.News.Model;
using Telerik.Web.UI;
using Telerik.Sitefinity.Web.UI.ControlDesign;
using Telerik.Sitefinity.Localization;
using Telerik.Sitefinity;
using Telerik.Sitefinity.Modules.Pages.Web.UI;
using Telerik.Sitefinity.Web.DataResolving;
using Telerik.Sitefinity.Modules.News.Web.UI;
using Telerik.Sitefinity.Pages.Model;
using Telerik.Sitefinity.GenericContent.Model;

namespace SocialWidgetsContrib.Widgets.Socialbar
{
    [ControlDesigner(typeof(SocialShareBarDesigner)), PropertyEditorTitle("Social Share Bar Editor")]
    [RequireScriptManager]
    public class SocialBarWidget: SimpleView
    {
        #region Backing Fields

        //we default everything to true so that you only need to disable what you don't want when embedding this in a widget template.

        private bool _showDelicious = true;
        private bool _showDigg = true;
        private bool _showEmail = true;
        private bool _showFacebook = true;
        private bool _showFurl = true;
        private bool _showReddit = true;
        private bool _showStumble = true;
        private bool _showTwitter = true;
        private string _untitledPage = "untitled page";
        private string _windowOption = "NewBrowser";

        #endregion

        #region Public Properties

        public bool ShowDelicious
        {
            get { return _showDelicious; }
            set { _showDelicious = value; }
        }

        public bool ShowDigg
        {
            get { return _showDigg; }
            set { _showDigg = value; }
        }

        public bool ShowReddit
        {
            get { return _showReddit; }
            set { _showReddit = value; }
        }

        public bool ShowStumble
        {
            get { return _showStumble; }
            set { _showStumble = value; }
        }

        public bool ShowFacebook
        {
            get { return _showFacebook; }
            set { _showFacebook = value; }
        }

        public bool ShowFurl
        {
            get { return _showFurl; }
            set { _showFurl = value; }
        }

        public bool ShowTwitter
        {
            get { return _showTwitter; }
            set { _showTwitter = value; }
        }

        public bool ShowEmail
        {
            get { return _showEmail; }
            set { _showEmail = value; }
        }

        public string UntitledPageTitle
        {
            get { return _untitledPage; }
            set { _untitledPage = value; }
        }

        public string WindowOption
        {
            get { return _windowOption; }
            set { _windowOption = value; }
        }

        #endregion

        #region Constants

        private const string DeliciousUrl = "http://del.icio.us/post?url={0}&amp;title={1}";

        private const string DiggUrl = "http://digg.com/submit?url={0}&amp;title={1}";

        private const string RedditUrl = "http://www.reddit.com/submit?url={0}&amp;title={1}";

        private const string StumbleUrl = "http://www.stumbleupon.com/submit?url={0}&amp;title={1}";

        private const string FaceBookUrl = "http://www.facebook.com/sharer.php?u={0}&amp;t={1}";

        private const string FurlUrl = "http://www.furl.net/storeIt.jsp?u={0}&amp;title={1}";

        private const string TwitterUrl = "";

        private const string EmailUrl = "mailto://?subject={0}&amp;title={1}";

        #endregion

        protected override void InitializeControls(GenericContainer container)
        {
            if (_showDelicious)
                litDelicious.Text = LinkAddressBuilder(DeliciousUrl, "barlink_delicious", "del.icio.us",
                                                       this.IsDesignMode());
            if (_showDigg)
                litDigg.Text = LinkAddressBuilder(DiggUrl, "barlink_digg", "Digg This", this.IsDesignMode());

            if (_showReddit)
                litReddit.Text = LinkAddressBuilder(RedditUrl, "barlink_reddit", "Reddit", this.IsDesignMode());

            if (_showStumble)
                litStumble.Text = LinkAddressBuilder(StumbleUrl, "barlink_stumble", "StumbleUpon", this.IsDesignMode());

            if (_showFacebook)
                litFacebook.Text = LinkAddressBuilder(FaceBookUrl, "barlink_facebook", "Facebook", this.IsDesignMode());

            if (_showFurl)
                litFurl.Text = LinkAddressBuilder(FurlUrl, "barlink_furl", "Furl", this.IsDesignMode());

            if (_showTwitter)
                litTwitter.Text = LinkAddressBuilder(TwitterUrl, "barlink_twitter", "Twitter", this.IsDesignMode());

            if (_showEmail)
                litEmail.Text = LinkAddressBuilder(EmailUrl, "barlink_email", "Email This", this.IsDesignMode());
        }

        protected override void OnLoad(EventArgs e)
        {
            
        }

        private string LinkAddressBuilder(string urlString, string className, string urlTitle, bool generateBlankUrl = false)
        {
            var szUrlTranslated = generateBlankUrl
                                      ? "#"
                                      : string.Format(urlString, HttpContext.Current.Server.UrlEncode(HttpContext.Current.Request.Url.AbsoluteUri),
                                                      HttpContext.Current.Server.UrlEncode(Parent.Page.Title ?? _untitledPage));
            var szOutput = new StringBuilder();

            szOutput.AppendFormat("<li class='{0} sbItem' onclick=\"openWindow($(this).find(':first-child').attr('href'));\">", className);
            szOutput.AppendFormat("<a href='{0}' target='{2}' title='{1}'></a>", szUrlTranslated, urlTitle, WindowOption == "NewBrowser" ? "_blank" : "SocialbarWindow");
            szOutput.Append("</li>");

            return szOutput.ToString();
        }

        protected override string LayoutTemplateName
        {
            get { return "SocialWidgetsContrib.Resources.Views.Socialbar.Socialbar" + WindowOption + ".ascx"; }
        }

        protected virtual Literal litDelicious
        {
            get { return Container.GetControl<Literal>("litDelicious", true); }
        }

        protected virtual Literal litDigg
        {
            get { return Container.GetControl<Literal>("litDigg", true); }
        }

        protected virtual Literal litReddit
        {
            get { return Container.GetControl<Literal>("litReddit", true); }
        }

        protected virtual Literal litStumble
        {
            get { return Container.GetControl<Literal>("litStumble", true); }
        }

        protected virtual Literal litFacebook
        {
            get { return Container.GetControl<Literal>("litFacebook", true); }
        }

        protected virtual Literal litFurl
        {
            get { return Container.GetControl<Literal>("litFurl", true); }
        }

        protected virtual Literal litTwitter
        {
            get { return Container.GetControl<Literal>("litTwitter", true); }
        }

        protected virtual Literal litEmail
        {
            get { return Container.GetControl<Literal>("litEmail", true); }
        }
    }
}